/*
 *------------------------------------------------------------------------------
 * @File      :    sc2335_mipi.c
 * @Date      :    2021-3-20
 * @Author    :    lomboswer <lomboswer@lombotech.com>
 * @Brief     :    Source file for MDP(Media Development Platform).
 *
 * Copyright (C) 2020-2021, LomboTech Co.Ltd. All rights reserved.
 *------------------------------------------------------------------------------
 */

#include "ei_comm_camera.h"
#include "camera_usr.h"

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif /* __cplusplus */

#define SNS_ID 2335
#define SNS_NAME  "sc2335_mipi"
#define DEV_TYPE MCSI

static VISS_DEV gaVissDev[ISP_MAX_DEV_NUM] = {-1};
static SNS_STATE_S g_sastSns[VISS_MAX_DEV_NUM];

static const SNS_REG_S s_aRaw1080p2Lane[] = {
    { 0x0103, 0x01 },
    { 0x0100, 0x00 },
    { 0x36e9, 0x80 },
    { 0x36f9, 0x80 },
    { 0x301f, 0x02 },
    { 0x3207, 0x3f },
    { 0x3249, 0x0f },
    { 0x3253, 0x08 },
    { 0x3271, 0x00 },
    { 0x3273, 0x03 },
    { 0x3301, 0x06 },
    { 0x3302, 0x09 },
    { 0x3304, 0x28 },
    { 0x3306, 0x30 },
    { 0x330b, 0x94 },
    { 0x330c, 0x08 },
    { 0x330d, 0x18 },
    { 0x330e, 0x14 },
    { 0x330f, 0x05 },
    { 0x3310, 0x06 },
    { 0x3314, 0x96 },
    { 0x3316, 0x00 },
    { 0x331e, 0x21 },
    { 0x332b, 0x08 },
    { 0x3333, 0x10 },
    { 0x3338, 0x80 },
    { 0x333a, 0x04 },
    { 0x334c, 0x04 },
    { 0x335f, 0x04 },
    { 0x3364, 0x17 },
    { 0x3366, 0x62 },
    { 0x337c, 0x05 },
    { 0x337d, 0x09 },
    { 0x337e, 0x00 },
    { 0x3390, 0x08 },
    { 0x3391, 0x18 },
    { 0x3392, 0x38 },
    { 0x3393, 0x09 },
    { 0x3394, 0x20 },
    { 0x3395, 0x20 },
    { 0x33a2, 0x07 },
    { 0x33ac, 0x04 },
    { 0x33ae, 0x14 },
    { 0x3614, 0x00 },
    { 0x3622, 0x16 },
    { 0x3630, 0x68 },
    { 0x3631, 0x84 },
    { 0x3637, 0x20 },
    { 0x363a, 0x1f },
    { 0x363c, 0x0e },
    { 0x3670, 0x0e },
    { 0x3674, 0xa1 },
    { 0x3675, 0x9c },
    { 0x3676, 0x9e },
    { 0x3677, 0x84 },
    { 0x3678, 0x85 },
    { 0x3679, 0x87 },
    { 0x367c, 0x18 },
    { 0x367d, 0x38 },
    { 0x367e, 0x08 },
    { 0x367f, 0x18 },
    { 0x3690, 0x32 },
    { 0x3691, 0x32 },
    { 0x3692, 0x44 },
    { 0x369c, 0x08 },
    { 0x369d, 0x38 },
    { 0x3908, 0x82 },
    { 0x391f, 0x18 },
    { 0x3e01, 0x8c },
    { 0x3e02, 0x00 },
    { 0x3e03, 0x0b },/* gain mode */
    { 0x3f00, 0x0d },
    { 0x3f04, 0x02 },
    { 0x3f05, 0x0e },
    { 0x3f09, 0x48 },
    { 0x4505, 0x0a },
    { 0x4509, 0x20 },
    { 0x481d, 0x0a },
    { 0x4827, 0x03 },
    { 0x5787, 0x10 },
    { 0x5788, 0x06 },
    { 0x578a, 0x10 },
    { 0x578b, 0x06 },
    { 0x5790, 0x10 },
    { 0x5791, 0x10 },
    { 0x5792, 0x00 },
    { 0x5793, 0x10 },
    { 0x5794, 0x10 },
    { 0x5795, 0x00 },
    { 0x5799, 0x00 },
    { 0x57c7, 0x10 },
    { 0x57c8, 0x06 },
    { 0x57ca, 0x10 },
    { 0x57cb, 0x06 },
    { 0x57d1, 0x10 },
    { 0x57d4, 0x10 },
    { 0x57d9, 0x00 },
    { 0x36e9, 0x20 },
    { 0x36f9, 0x27 },
    //{ 0x0100, 0x01 },
};

static CAMERA_POWER_ACT_S s_stPowerUpAct[] = {
    {SNS_PWDN, SNS_DOWN},
    {SNS_DELAY, 5},
    {SNS_RESET, SNS_DOWN},
    {SNS_DELAY, 20},
    {SNS_RESET, SNS_UP},
    {SNS_DELAY, 20},
    {SNS_PWDN, SNS_UP},
};

static CAMERA_POWER_ACT_S s_sPowerDownAct[] = {
    {SNS_PWDN, SNS_DOWN},
    {SNS_DELAY, 5},
    {SNS_RESET, SNS_DOWN},
    {SNS_DELAY, 5},
};

static SNS_TYPE_ATTR_S s_astSnsAttr[] = {
    {
        .enSnsType                  = SC2335_MIPI_1920_1080_30FPS_RAW10,
        .enPixelFormat              = PIX_FMT_RGB_BAYER_10BITS,
        .enRgbSeq                   = VISS_RGB_SEQ_BGGR,
        .stSize                     = {1920, 1080},
        .u32Mclock                  = 27000000,
        .enWorkMode                 = VISS_WORK_MODE_1Chn,

        .u32IspClk                  = 160000000,
        .u32IppuClk                 = 160000000,
        .u32Pclk                    = 70875000,
        .u32Vts                     = 1125,
        .u32Hts                     = 2100,

        .u32FrameRate               = 30,
        .astRegCfgs[0].pstRegs      = s_aRaw1080p2Lane,
        .astRegCfgs[0].u32RegsCnt   = ARRAY_SIZE(s_aRaw1080p2Lane),

        .stDevCfg.stMcsiCfg.u32PhyFreq  = 404352000,
        .stDevCfg.stMcsiCfg.enDlanes    = VISS_LANE_NUM_2Lane,
    },
};

static int CAMERA_SetI2cInfo(VISS_DEV dev)
{
    SNS_STATE_S *pstSnsState = EI_NULL;

    SNS_GET_CTX(dev, pstSnsState);
    CAMERA_CHECK_POINTER(pstSnsState);

    if (pstSnsState->stI2cInfo.fd > 0) {
        return EI_SUCCESS;
    }

    pstSnsState->stI2cInfo.u8RegBits = 16;
    pstSnsState->stI2cInfo.u8ValBits = 8;
    pstSnsState->stI2cInfo.u16I2cBusNum = 4;
    pstSnsState->stI2cInfo.u16I2cDevAddr = 0x30;
    pstSnsState->pcName = SNS_NAME;

    return EI_SUCCESS;
}

static EI_S32 CAMERA_Detect(VISS_DEV VissDev)
{
    EI_U8 u8Tmp[2];
    EI_S32 s32Ret = EI_SUCCESS;
    EI_U16 u16Id;
    SNS_STATE_S *pstSnsState = EI_NULL;
    I2C_INFO_S *pstI2cInfo = EI_NULL;

    SNS_GET_CTX(VissDev, pstSnsState);

    pstI2cInfo = &pstSnsState->stI2cInfo;

    CAMERA_CHECK_POINTER(pstI2cInfo->pfnReadReg);
    s32Ret = pstI2cInfo->pfnReadReg(pstSnsState, 0x3107, &u8Tmp[0]);
    if (s32Ret != 0) {
        PRT_VISS_ERR("tmp[0]=%x\n", u8Tmp[0]);
        return s32Ret;
    }
    s32Ret = pstI2cInfo->pfnReadReg(pstSnsState, 0x3108, &u8Tmp[1]);
    if (s32Ret != 0) {
        PRT_VISS_ERR("tmp[0]=%x,tmp[1]=%x\n", u8Tmp[0], u8Tmp[1]);
        return s32Ret;
    }
    u16Id = (u8Tmp[0] << 8) | u8Tmp[1];
    PRT_VISS_INFO("%s id: 0x%04x\n", SNS_NAME, u16Id);

    if (u16Id != 0xcb14) {
        PRT_VISS_ERR("ID wrong! (0x%04x != 0xcb14)\n", u16Id);
        return EI_FAILURE;
    }

    return EI_SUCCESS;
}

static EI_S32 CAMERA_Stream(VISS_DEV dev, EI_BOOL bOn)
{
    SNS_STATE_S *pstSnsState = EI_NULL;
    EI_S32 s32Ret = EI_SUCCESS;
    SNS_GET_CTX(dev, pstSnsState);
    CAMERA_CHECK_POINTER(pstSnsState);
    PRT_VISS_ERR("%s CAMERA_Stream-------%d\n", SNS_NAME, bOn);

    if (bOn) {
        s32Ret = pstSnsState->stI2cInfo.pfnWriteReg(pstSnsState, 0x0100, 0x01);
        if (s32Ret != EI_SUCCESS) {
            PRT_VISS_ERR("CAMERA_Stream---------%d\n", bOn);
        }
    } else {
        s32Ret = pstSnsState->stI2cInfo.pfnWriteReg(pstSnsState, 0x0100, 0x00);
        if (s32Ret != EI_SUCCESS) {
            PRT_VISS_ERR("CAMERA_Stream---------%d\n", bOn);
        }
    }
    return s32Ret;

}

/* The following functions can be modified as needed */

static int CAMERA_I2cInit(VISS_DEV dev)
{
    SNS_STATE_S *pstSnsState = EI_NULL;
    int ret = EI_SUCCESS;

    SNS_GET_CTX(dev, pstSnsState);
    CAMERA_CHECK_POINTER(pstSnsState);

    ret = CAMERA_SetI2cInfo(dev);
    CAMERA_CHECK_SUCCESS(ret);

    ret = CAMERA_I2C_Reg_Ops(&pstSnsState->stI2cInfo);
    CAMERA_CHECK_SUCCESS(ret);

    return EI_SUCCESS;
}

static int CAMERA_I2cExit(VISS_DEV dev)
{
    SNS_STATE_S *pstSnsState = EI_NULL;

    SNS_GET_CTX(dev, pstSnsState);
    CAMERA_CHECK_POINTER(pstSnsState);

    return EI_SUCCESS;
}

static EI_S32 CAMERA_PowerUp(VISS_DEV VissDev)
{
    SNS_STATE_S *pstSnsState = EI_NULL;

    SNS_GET_CTX(VissDev, pstSnsState);

    CAMERA_SensorPowerUp(VissDev, pstSnsState);

    return 0;
}

static EI_S32 CAMERA_PowerDown(VISS_DEV VissDev)
{
    SNS_STATE_S *pstSnsState = EI_NULL;

    SNS_GET_CTX(VissDev, pstSnsState);

    CAMERA_SensorPowerDown(VissDev, pstSnsState);

    return 0;
}

static EI_S32 CAMERA_MirrorFlip(VISS_DEV VissDev)
{
    SNS_STATE_S *pstSnsState = EI_NULL;
    I2C_INFO_S *pstI2cInfo = EI_NULL;
    EI_S32 s32Ret = EI_SUCCESS;
    EI_U32 u32Tmp = 0;

    SNS_GET_CTX(VissDev, pstSnsState);
    CAMERA_CHECK_POINTER(pstSnsState);

    pstI2cInfo = &pstSnsState->stI2cInfo;

    PRT_VISS_INFO("%s set mirror[%d] flip[%d] \n", SNS_NAME,
        pstSnsState->u32SnsMirror, pstSnsState->u32SnsFlip);

    if (!pstSnsState->u32SnsMirror && !pstSnsState->u32SnsFlip) {
        s32Ret = pstI2cInfo->pfnWriteReg(pstSnsState, 0x3221, (u32Tmp & 0x99 ) | 0x00);
        if (s32Ret != 0) {
            PRT_VISS_ERR("%s pfnWriteReg error\n", SNS_NAME);
            return s32Ret;
        }
        return s32Ret;
    }

    /* sensor flip & mirror */
    pstI2cInfo->pfnReadReg(pstSnsState, 0x3221, &u32Tmp);
    if (s32Ret != 0) {
        PRT_VISS_ERR("%s pfnReadReg error\n", SNS_NAME);
        return s32Ret;
    }

    if (pstSnsState->u32SnsMirror) {
        s32Ret = pstI2cInfo->pfnWriteReg(pstSnsState, 0x3221, (u32Tmp & 0xf9 ) | 0x06);
    } else {
        s32Ret = pstI2cInfo->pfnWriteReg(pstSnsState, 0x3221, (u32Tmp & 0xf9 ));
    }
    if (s32Ret != 0) {
        PRT_VISS_ERR("%s pfnWriteReg error\n", SNS_NAME);
        return s32Ret;
    }

    pstI2cInfo->pfnReadReg(pstSnsState, 0x3221, &u32Tmp);
    if (s32Ret != 0) {
        PRT_VISS_ERR("%s pfnReadReg error\n", SNS_NAME);
        return s32Ret;
    }

    if (pstSnsState->u32SnsFlip) {
        s32Ret = pstI2cInfo->pfnWriteReg(pstSnsState, 0x3221, (u32Tmp & 0x9f ) | 0x60);
    } else {
        s32Ret = pstI2cInfo->pfnWriteReg(pstSnsState, 0x3221, (u32Tmp & 0x9f ));
    }
    if (s32Ret != 0) {
        PRT_VISS_ERR("%s pfnWriteReg error\n", SNS_NAME);
        return s32Ret;
    }

    return s32Ret;
}

static EI_S32 CAMERA_SetGain(ISP_DEV dev, EI_FLOAT fGain);
static EI_S32 CAMERA_SetExpTime(ISP_DEV dev, EI_FLOAT *afIntTime);

static EI_S32 CAMERA_Init(VISS_DEV dev)
{
    EI_U32 j;
    const SNS_TYPE_ATTR_S *pstSnsType;
    SNS_STATE_S *pstSnsState = EI_NULL;
    EI_S32 s32Ret;

    SNS_GET_CTX(dev, pstSnsState);
    CAMERA_CHECK_POINTER(pstSnsState);

    pstSnsType = pstSnsState->pstSnsAttr;
    for (j = 0; j < MAX_COF; j++) {
        s32Ret = pstSnsState->stI2cInfo.pfnwriteRegList(pstSnsState,
                pstSnsType->astRegCfgs[j].pstRegs, pstSnsType->astRegCfgs[j].u32RegsCnt);
        if (s32Ret != EI_SUCCESS) {
            PRT_VISS_ERR("%s writeRegList err", SNS_NAME);
            return s32Ret;
        }
    }

    if ((pstSnsState->afIntTime[0] > 0.000001) && (pstSnsState->fGain >= 1.0)) {
        CAMERA_SetExpTime(pstSnsState->IspDev, pstSnsState->afIntTime);
        CAMERA_SetGain(pstSnsState->IspDev, pstSnsState->fGain);
    }

    CAMERA_MirrorFlip(dev);

    PRT_VISS_INFO("%s writeRegList success", SNS_NAME);

    return s32Ret;
}

static void CAMERA_Exit(VISS_DEV dev)
{
    CAMERA_I2cExit(dev);

    return;
}

static EI_S32 CAMERA_SetImageMode(VISS_DEV dev, SNS_TYPE_E enSnsType)
{
    SNS_STATE_S *pstSnsState = EI_NULL;

    SNS_GET_CTX(dev, pstSnsState);
    CAMERA_CHECK_POINTER(pstSnsState);

    pstSnsState->enSnsType = enSnsType;
    pstSnsState->bSyncInit = EI_FALSE;

    return EI_SUCCESS;
}

static EI_S32 CAMERA_EnumSnstype(VISS_DEV VissDev, EI_U32 u32Index, SNS_TYPE_E *peSnstype)
{
    const SNS_TYPE_ATTR_S *pstSnsType;

    if (u32Index >= ARRAY_SIZE(s_astSnsAttr))
        return EN_ERR_NO_FOUND;

    pstSnsType = &s_astSnsAttr[u32Index];

    *peSnstype = pstSnsType->enSnsType;

    return EI_SUCCESS;
}

static EI_S32 CAMERA_InitSensorExpFunction(SENSOR_EXP_FUNC_S *pstSensorExpFunc)
{
    CAMERA_CHECK_POINTER(pstSensorExpFunc);

    memset(pstSensorExpFunc, 0, sizeof(SENSOR_EXP_FUNC_S));

    pstSensorExpFunc->pfnSensorPowerUp  = CAMERA_PowerUp;
    pstSensorExpFunc->pfnSensorPowerDown = CAMERA_PowerDown;
    pstSensorExpFunc->pfnSensorDetect   = CAMERA_Detect;
    pstSensorExpFunc->pfnSensorInit     = CAMERA_Init;
    pstSensorExpFunc->pfnSensorStream   = CAMERA_Stream;
    pstSensorExpFunc->pfnSensorExit     = CAMERA_Exit;
    pstSensorExpFunc->pfnSensorSetImageMode   = CAMERA_SetImageMode;
    pstSensorExpFunc->pfnSensorEnumSnstype   = CAMERA_EnumSnstype;
    pstSensorExpFunc->pfnSensorSetMirrorFlip = CAMERA_MirrorFlip;

    return EI_SUCCESS;
}

static EI_S32 CAMERA_GetAeDefault(ISP_DEV dev, AE_SENSOR_DEFAULT_S *pstAeSnsDft)
{
    CAMERA_CHECK_POINTER(pstAeSnsDft);

    return EI_SUCCESS;
}

static EI_S32 CAMERA_SetFps(ISP_DEV dev, EI_FLOAT f32Fps, AE_SENSOR_DEFAULT_S *pstAeSnsDft)
{
    CAMERA_CHECK_POINTER(pstAeSnsDft);

    return EI_SUCCESS;
}

static EI_S32 CAMERA_SetExpTime(ISP_DEV dev, EI_FLOAT *afIntTime)
{
    SNS_STATE_S *pstSnsState = EI_NULL;
    EI_S32 s32Ret = EI_SUCCESS;
    EI_U32 u32Pclk, u32Hts, u32Vts;
    EI_U32 shutter_time;
    VISS_DEV Dev;
    //return EI_SUCCESS;

    SNS_GET_ISP_TO_VISS(dev, Dev);

    SNS_GET_CTX(Dev, pstSnsState);
    CAMERA_CHECK_POINTER(pstSnsState);
    CAMERA_CHECK_POINTER(afIntTime);
    pstSnsState->afIntTime[0] = afIntTime[0];
    pstSnsState->afIntTime[1] = afIntTime[1];
    pstSnsState->afIntTime[2] = afIntTime[2];
    pstSnsState->afIntTime[3] = afIntTime[3];
    pstSnsState->IspDev = dev;
    CAMERA_CHECK_POWERON(pstSnsState);

    u32Pclk = pstSnsState->pstSnsAttr->u32Pclk;
    u32Hts = pstSnsState->pstSnsAttr->u32Hts;
    u32Vts = pstSnsState->pstSnsAttr->u32Vts;

    if ((u32Pclk == 0) || (u32Hts == 0) || (u32Vts == 0)) {
        PRT_VISS_ERR("error! u32Pclk %d, u32Hts %d, u32Vts %d\n", u32Pclk, u32Hts, u32Vts);
        return EI_FAILURE;
    }

    /* afIntTime[0] long/medium/short/vshort exptime (s) */
    shutter_time = afIntTime[0] * u32Pclk / u32Hts * 2;

    if (shutter_time > (2 * u32Vts - 10))
        shutter_time = (2 * u32Vts - 10);

    s32Ret += pstSnsState->stI2cInfo.pfnWriteReg(pstSnsState, 0x3e00, ((shutter_time >> 12) & 0x0f));
    s32Ret += pstSnsState->stI2cInfo.pfnWriteReg(pstSnsState, 0x3e01, ((shutter_time >> 4) & 0xff));
    s32Ret += pstSnsState->stI2cInfo.pfnWriteReg(pstSnsState, 0x3e02, ((shutter_time << 4) & 0xf0));
    if (s32Ret != EI_SUCCESS) {
        PRT_VISS_ERR("\n");
    }

    afIntTime[0] = (EI_FLOAT)(shutter_time / 2) * u32Hts / u32Pclk;
    if (afIntTime[0] < 0.00001) {
        PRT_VISS_WARN("Warning! afIntTime[0] < 0.00001, now force 0.00001 \n");
        afIntTime[0] = 0.00001;
    }

    return EI_SUCCESS;
}

static EI_S32 CAMERA_SetGain(ISP_DEV dev, EI_FLOAT fGain)
{
	EI_U32 again_fine, dgain_fine;
	EI_U32 again = 0, dgain = 0;
    EI_U32 gain;
    EI_S32 s32Ret = EI_SUCCESS;
    SNS_STATE_S *pstSnsState = EI_NULL;
    VISS_DEV Dev;

    SNS_GET_ISP_TO_VISS(dev, Dev);
    SNS_GET_CTX(Dev, pstSnsState);

    CAMERA_CHECK_POINTER(pstSnsState);
    pstSnsState->fGain = fGain;
    CAMERA_CHECK_POWERON(pstSnsState);

    gain = fGain * 1024;

	if (gain < 2 * 1024) {
		again = 3;
		again_fine = gain >> 4;
		dgain = 0;
		dgain_fine = (gain * 8 / again_fine);
	} else if (gain <  4 * 1024) {
		again = 7;
		again_fine = gain >> 5;
		dgain = 0;
		dgain_fine = (gain * 4 / again_fine);
	} else if (gain < 8 * 1024) {
		again = 0xf;
		again_fine = gain >> 6;
		dgain = 0;
		dgain_fine = (gain * 2 / again_fine);
	} else if (gain <= 15.875 * 1024) {
		again = 0x1f;
		again_fine = gain >> 7;
		dgain = 0;
		dgain_fine = (gain / again_fine);
	} else if (gain < 31.75 * 1024) {
		again = 0x1f;
		again_fine = 0x7f;
		dgain = 0;
		dgain_fine = gain / 127;
	} else if (gain < 63.5 * 1024) {
		again = 0x1f;
		again_fine = 0x7f;
		dgain = 1;
		dgain_fine = gain / 254;
	} else if (gain < 127 * 1024) {
		again = 0x1f;
		again_fine = 0x7f;
		dgain = 3;
		dgain_fine = gain / 508;
	} else if (gain < 254 * 1024) {
		again = 0x1f;
		again_fine = 0x7f;
		dgain = 7;
		dgain_fine = gain / 1016;
	} else if (gain <= 15.875 * 31.75 * 1024) {
		again = 0x1f;
		again_fine = 0x7f;
		dgain = 0xF;
		dgain_fine = gain / 2032;
	} else {
		again = 0x1f;
		again_fine = 0x7f;
		dgain = 0xF;
		dgain_fine = 254;
	}

    s32Ret += pstSnsState->stI2cInfo.pfnWriteReg(pstSnsState, 0x3e09, again_fine);
    s32Ret += pstSnsState->stI2cInfo.pfnWriteReg(pstSnsState, 0x3e08, again);
    s32Ret += pstSnsState->stI2cInfo.pfnWriteReg(pstSnsState, 0x3e07, dgain_fine);
    s32Ret += pstSnsState->stI2cInfo.pfnWriteReg(pstSnsState, 0x3e06, dgain);
    if (s32Ret != EI_SUCCESS) {
        PRT_VISS_ERR("\n");
    }

    s32Ret += pstSnsState->stI2cInfo.pfnWriteReg(pstSnsState, 0x3812, 0x00);

	if (fGain < 2)
        s32Ret += pstSnsState->stI2cInfo.pfnWriteReg(pstSnsState, 0x363c, 0x0f);
	else
        s32Ret += pstSnsState->stI2cInfo.pfnWriteReg(pstSnsState, 0x363c, 0x07);

    s32Ret += pstSnsState->stI2cInfo.pfnWriteReg(pstSnsState, 0x3812, 0x30);


    return EI_SUCCESS;
}

static EI_S32 CAMERA_InitAeExpFunction(AE_SENSOR_EXP_FUNC_S *pstExpFuncs)
{
    CAMERA_CHECK_POINTER(pstExpFuncs);

    memset(pstExpFuncs, 0, sizeof(AE_SENSOR_EXP_FUNC_S));

    pstExpFuncs->pfnGetAeDefault    = CAMERA_GetAeDefault;
    pstExpFuncs->pfnSetFps          = CAMERA_SetFps;
    pstExpFuncs->pfnSetExpTime      = CAMERA_SetExpTime;
    pstExpFuncs->pfnSetGain         = CAMERA_SetGain;

    return EI_SUCCESS;
}

static EI_S32 CAMERA_GetAwbDefault(ISP_DEV dev, AWB_SENSOR_DEFAULT_S *pstAwbSnsDft)
{
    CAMERA_CHECK_POINTER(pstAwbSnsDft);

    return EI_SUCCESS;
}

static EI_S32 CAMERA_InitAwbExpFunction(AWB_SENSOR_EXP_FUNC_S *pstExpFuncs)
{
    CAMERA_CHECK_POINTER(pstExpFuncs);

    memset(pstExpFuncs, 0, sizeof(AWB_SENSOR_EXP_FUNC_S));
    pstExpFuncs->pfnGetAwbDefault = CAMERA_GetAwbDefault;

    return EI_SUCCESS;
}

/****************************************************************************
 * callback structure                                                       *
 ****************************************************************************/

static EI_S32 CAMERA_CtxInit(VISS_DEV dev)
{
    SNS_STATE_S *pastSnsStateCtx = EI_NULL;

    SNS_GET_CTX(dev, pastSnsStateCtx);

    CAMERA_CHECK_POINTER(pastSnsStateCtx);

    memset(pastSnsStateCtx, 0, sizeof(SNS_STATE_S));

    pastSnsStateCtx->pstPowerUpAct      = s_stPowerUpAct;
    pastSnsStateCtx->pstPowerDownAct    = s_sPowerDownAct;
    pastSnsStateCtx->u8PwrUpActs        = ARRAY_SIZE(s_stPowerUpAct);
    pastSnsStateCtx->u8PwrDnActs        = ARRAY_SIZE(s_sPowerDownAct);

    return EI_SUCCESS;
}

static EI_VOID CAMERA_CtxExit(VISS_DEV dev)
{
    return ;
}

static EI_S32 CAMERA_RegisterVissCallback(VISS_DEV dev, SNS_TYPE_E enType)
{
    EI_S32 s32Ret;
    SENSOR_S stSns = {0};
    EI_U32 i;
    SNS_STATE_S *pstSnsState = EI_NULL;

    s32Ret = CAMERA_CtxInit(dev);
    if (EI_SUCCESS != s32Ret) {
        return EI_FAILURE;
    }

    SNS_GET_CTX(dev, pstSnsState);

    for (i = 0; i < ARRAY_SIZE(s_astSnsAttr); i++) {
        if (enType == s_astSnsAttr[i].enSnsType) {
            pstSnsState->pstSnsAttr = &s_astSnsAttr[i];
            break;
        }
    }

    pstSnsState->enSnsType = enType;
    pstSnsState->Dev = dev;

    if (i >= ARRAY_SIZE(s_astSnsAttr)) {
        PRT_VISS_ERR("%s does not support this chn configuration", SNS_NAME);
        return EI_FAILURE;
    }

    s32Ret = CAMERA_I2cInit(dev);
    if (EI_SUCCESS != s32Ret) {
        return EI_FAILURE;
    }

    stSns.stSnsAttrInfo.eSensorId = SNS_ID;
    stSns.stSnsAttrInfo.enDevType = DEV_TYPE;
    stSns.stSnsAttrInfo.pstSns = pstSnsState;

    s32Ret  = CAMERA_InitSensorExpFunction(&stSns.stSnsAttrInfo.stSnsExp);
    s32Ret |= VISS_SensorRegCallBack(dev, &stSns);
    if (EI_SUCCESS != s32Ret) {
        PRT_VISS_ERR("%s sensor register callback function failed!\n", SNS_NAME);
        return s32Ret;
    }

    s32Ret = CAMERA_OpenI2c(pstSnsState);
    CAMERA_CHECK_SUCCESS(s32Ret);

    return EI_SUCCESS;
}

static EI_S32 CAMERA_UnregisterVissCallback(VISS_DEV dev)
{
    EI_S32 s32Ret;
    SNS_STATE_S *pstSnsState = EI_NULL;

    SNS_GET_CTX(dev, pstSnsState);

    s32Ret = CAMERA_ExitI2c(pstSnsState);
    CAMERA_CHECK_SUCCESS(s32Ret);

    s32Ret = VISS_SensorUnRegCallBack(dev, SNS_ID);
    if (EI_SUCCESS != s32Ret) {
        PRT_VISS_ERR("%s sensor unregister callback function failed!\n", SNS_NAME);
        return s32Ret;
    }

    CAMERA_CtxExit(dev);

    return EI_SUCCESS;
}

static EI_S32 CAMERA_RegisterIspCallback(VISS_DEV Dev, ISP_DEV dev, ISP_3ALIB_S *pstAeLib, ISP_3ALIB_S *pstAwbLib)
{
    EI_S32 s32Ret;
    AE_SENSOR_REGISTER_S  stAeRegister;
    AWB_SENSOR_REGISTER_S stAwbRegister;

    CAMERA_CHECK_POINTER(pstAeLib);
    CAMERA_CHECK_POINTER(pstAwbLib);

    SNS_SET_ISP_TO_VISS(dev, Dev);

    stAeRegister.SnsId = SNS_ID;
    stAwbRegister.SnsId = SNS_ID;

    s32Ret  = CAMERA_InitAeExpFunction(&stAeRegister.stSnsExp);
    s32Ret |= EI_MI_AE_SensorRegCallBack(dev, pstAeLib, &stAeRegister);

    if (EI_SUCCESS != s32Ret) {
        EI_TRACE_ISP(EI_DBG_ERR, "%s register callback function to ae lib failed!\n", SNS_NAME);
        return s32Ret;
    }

    s32Ret  = CAMERA_InitAwbExpFunction(&stAwbRegister.stSnsExp);
    s32Ret |= EI_MI_AWB_SensorRegCallBack(dev, pstAwbLib, &stAwbRegister);

    if (EI_SUCCESS != s32Ret) {
        EI_TRACE_ISP(EI_DBG_ERR, "%s register callback function to awb lib failed!\n", SNS_NAME);
        return s32Ret;
    }

    return EI_SUCCESS;
}

static EI_S32 CAMERA_UnregisterIspCallback(ISP_DEV dev, ISP_3ALIB_S *pstAeLib, ISP_3ALIB_S *pstAwbLib)
{
    EI_S32 s32Ret;

    CAMERA_CHECK_POINTER(pstAeLib);
    CAMERA_CHECK_POINTER(pstAwbLib);

    s32Ret = EI_MI_AE_SensorUnRegCallBack(dev, pstAeLib, SNS_ID);
    if (EI_SUCCESS != s32Ret) {
        EI_TRACE_ISP(EI_DBG_ERR, "%s unregister callback function to ae lib failed!\n", SNS_NAME);
        return s32Ret;
    }

    s32Ret = EI_MI_AWB_SensorUnRegCallBack(dev, pstAwbLib, SNS_ID);
    if (EI_SUCCESS != s32Ret) {
        EI_TRACE_ISP(EI_DBG_ERR, "%s unregister callback function to awb lib failed!\n", SNS_NAME);
        return s32Ret;
    }

    return EI_SUCCESS;
}

static EI_S32 CAMERA_GetVissDevAttrBySns(SNS_TYPE_E enType,
    VISS_DEV_ATTR_S *pstVissDevAttr)
{
    EI_U32 i;
    const SNS_TYPE_ATTR_S *pstSnsType;

    for (i = 0; i < ARRAY_SIZE(s_astSnsAttr); i++) {
        if (enType == s_astSnsAttr[i].enSnsType) {
            pstSnsType = &s_astSnsAttr[i];
            pstVissDevAttr->stSize          = pstSnsType->stSize;
            pstVissDevAttr->enWorkMode      = pstSnsType->enWorkMode;
            pstVissDevAttr->mClock          = pstSnsType->u32Mclock;
            pstVissDevAttr->enPixelFormat   = pstSnsType->enPixelFormat;
            pstVissDevAttr->enWdrMode       = pstSnsType->enWdrMode;
            pstVissDevAttr->enIspWdrMode    = pstSnsType->enIspWdrMode;
            pstVissDevAttr->enRgbSeq        = pstSnsType->enRgbSeq;
            pstVissDevAttr->u32ModuleClk    = pstSnsType->u32ModuleClk;
            pstVissDevAttr->u32Fps          = pstSnsType->u32FrameRate;
            pstVissDevAttr->u32IspClk       = pstSnsType->u32IspClk;
            pstVissDevAttr->u32IppuClk      = pstSnsType->u32IppuClk;
            pstVissDevAttr->pcSnsName       = SNS_NAME;
            pstVissDevAttr->stDevCfg        = pstSnsType->stDevCfg;
            return EI_SUCCESS;
        }
    }

    PRT_VISS_ERR("%s does not support this dev configuration", SNS_NAME);

    return EI_FAILURE;
}

static EI_S32 CAMERA_GetVissChnAttrBySns(SNS_TYPE_E enType,
    VISS_CHN_ATTR_S *pstVissChnAttr)
{
    EI_U32 i;
    const SNS_TYPE_ATTR_S *pstSnsType;

    for (i = 0; i < ARRAY_SIZE(s_astSnsAttr); i++) {
        if (enType == s_astSnsAttr[i].enSnsType) {
            pstSnsType = &s_astSnsAttr[i];
            pstVissChnAttr->stSize           = pstSnsType->stSize;
            pstVissChnAttr->enPixelFormat    = pstSnsType->enPixelFormat;
            pstVissChnAttr->stFrameRate.s32SrcFrameRate      = pstSnsType->u32FrameRate;
            return EI_SUCCESS;
        }
    }

    PRT_VISS_ERR("%s does not support this chn configuration", SNS_NAME);

    return EI_FAILURE;

}

CAMERA_OBJ_S stCameraSc2335mipiObj = {
    .pfnRegisterVissCallback    = CAMERA_RegisterVissCallback,
    .pfnUnRegisterVissCallback  = CAMERA_UnregisterVissCallback,
    .pfnRegisterIspCallback     = CAMERA_RegisterIspCallback,
    .pfnUnRegisterIspCallback   = CAMERA_UnregisterIspCallback,
    .pfnGetVissDevAttrBySns     = CAMERA_GetVissDevAttrBySns,
    .pfnGetVissChnAttrBySns     = CAMERA_GetVissChnAttrBySns,
};

#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* __cplusplus */

